$(function () {
    // new AjaxUpload('#upload', {
    //     action: baseURL + "sys/oss/upload",
    //     name: 'file',
    //     autoSubmit:true,
    //     responseType:"json",
    //     onSubmit:function(file, extension){
    //         if(vm.config.type == null){
    //             alert("云存储配置未配置");
    //             return false;
    //         }
    //         if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
    //             alert('只支持jpg、png、gif格式的图片！');
    //             return false;
    //         }
    //     },
    //     onComplete : function(file, r){
    //         if(r.code == 0){
    //             alert(r.url);
    //             vm.reload();
    //         }else{
    //             alert(r.msg);
    //         }
    //     }
    // });

});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        config: {},
        tableData: [],
        multipleSelection:[],
        currentPage: 1,
        total : 0,
        pageSize : 10,
        fileList:[],
        uploadPath : "/sys/oss/upload",
        dialogVisible:false,
        imgUrl:null,
    },
    created: function(){
        this.getConfig();
        axios({
            method: 'post',
            url: '/sys/oss/list',
        }).then(function (result) {
            vm.tableData = eval(result.data.data);
            vm.total = result.data.totalelements;
        });
    },
	methods: {
		query: function () {
            axios({
                method: 'post',
                url: '/sys/oss/list',
            }).then(function (result) {
                vm.tableData = eval(result.data.data);
                vm.total = result.data.totalelements;
            });
		},
		getConfig: function () {
            $.getJSON(baseURL + "sys/oss/config", function(r){
				vm.config = r.config;
            });
        },
		addConfig: function(){
			vm.showList = false;
			vm.title = "云存储配置";
		},
		saveOrUpdate: function () {
			var url = baseURL + "sys/oss/saveConfig";
			$.ajax({
				type: "POST",
			    url: url,
                contentType: "application/json",
			    data: JSON.stringify(vm.config),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
        del: function () {
            var list = this.multipleSelection;
            var ids = new Array();
            for (var i = 0; i < list.length; i++) {
                ids.push(list[i].id);
            }
            this.deleteFile(ids.join(","));
        },
		reload: function () {
            vm.showList = true;
            this.query();
		},
        handleCurrentChange:function(val) {
            console.info("选中的是：" + val);
            this.multipleSelection = val;
            console.info("选中的列表：" + this.multipleSelection);
        },
        handleDownload:function(index, row) {
            console.log(index, row.url);
            vm.dialogVisible = true;
            vm.imgUrl = row.url;
        },
        handleDelete:function(index, row) {
            this.deleteFile(row.id);
        },
        deleteFile : function (ids) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                axios({
                    method: 'post',
                    url: baseURL + 'sys/oss/delete',
                    params:{"ids" : ids}
                }).then(function (result) {
                    if(result.data.success){
                        vm.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                        vm.reload();
                    }else{
                        alert(result.data.message);
                    }
                });
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        handleSizeChange(val) {
            console.log("每页" + val + "条");
            vm.pageSize = val;
        },
        currentChange(val) {
            console.log("当前页: " + val);
            axios({
                method: 'post',
                url: '/sys/oss/list',
                params : {"page" : val, "pageSize" : vm.pageSize},
            }).then(function (result) {
                vm.tableData = eval(result.data.data);
                vm.total = result.data.totalelements;
            });
        },
        handleRemove(file, fileList) {
            console.log(file, fileList);
        },
        handlePreview(file) {
            console.log(file);
        },
        handleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        uploadSuccess:function (response, file, fileList) {
            this.query();
        },
	}
});